package org.csu.jpetstorespringversion.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.jpetstorespringversion.mapper.OrdersMapper;
import org.csu.jpetstorespringversion.model.Item;
import org.csu.jpetstorespringversion.model.LineItem;
import org.csu.jpetstorespringversion.model.Order;
import org.csu.jpetstorespringversion.model.Sequence;
import org.csu.jpetstorespringversion.service.LineItemService;
import org.csu.jpetstorespringversion.service.OrdersService;
import org.csu.jpetstorespringversion.service.SequenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Order> implements OrdersService {
    @Autowired
    SequenceService sequenceService;
    @Autowired
    ItemServiceImpl itemService;

    @Autowired
    LineItemServiceImpl lineItemService;
    @Autowired
   OrdersMapper ordersMapper;
    @Override
    public int getNextId(String name) {
          Sequence sequence = new Sequence(name, -1);
          Sequence parameterObject = new Sequence(name, sequence.getNextId() + 1);
          sequenceService.updateSequence(parameterObject);
          return sequence.getNextId();
    }
    @Override
    public void insertOrder(Order order) {
        order.setOrderId(getNextId("ordernum"));
        for (int i = 0; i < order.getLineItems().size(); i++) {
            LineItem lineItem = (LineItem) order.getLineItems().get(i);
            String itemId = lineItem.getItemId();
            Integer increment =lineItem.getQuantity();
            String increment1 = Integer.toString(increment);
            /**修改一下**/
            Map<String, String> param = new HashMap<String, String>(2);
            param.put("itemId", itemId);
            param.put("increment", increment1);
            itemService.updateInventoryQuantity(param);
        }

       //数据库insertOrder(order);
        int insertOrder =ordersMapper.insert(order);
        System.out.println("insertOrder="+insertOrder);
        //ordersService.insertOrderStatus(order);


        for (int i = 0; i < order.getLineItems().size(); i++) {
            LineItem lineItem = (LineItem) order.getLineItems().get(i);
            lineItem.setOrderId(order.getOrderId());
            lineItemService.insertLineItem(lineItem);
        }
    }
    @Override
    public Order getOrder(int orderId) {
            //Order order = ordersService.getOrder(orderId);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("orderid",orderId);
        Order order= ordersMapper.selectOne(wrapper);

        order.setLineItems(lineItemService.getLineItemsByOrderId(orderId));

            for (int i = 0; i < order.getLineItems().size(); i++) {
                LineItem lineItem = (LineItem) order.getLineItems().get(i);
                Item item = itemService.getItem(lineItem.getItemId());
                item.setQuantity(itemService.getInventoryQuantity(lineItem.getItemId()));
                lineItem.setItem(item);
            }

            return order;
    }

    @Override
    public List<Order> getOrdersByUsername(String username) {
        List<Order> orderList = new ArrayList<>();
        HashMap<String, Object> map=new HashMap<>();
        map.put("username",username);
        List<Order> orderList1=ordersMapper.selectByMap(map);
        return orderList1;
    }

    @Override
    public void insertOrderStatus(Order order) {

    }
}
